// pages/institucharge/institucharge.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
// const util=require('../../utils/util.js')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        banners: [
            urls.apiUrl+'up/admin/0_1562652058.jpg',
            urls.apiUrl+'up/admin/0_1562651956.jpg',
            urls.apiUrl+'up/admin/0_1562652081.jpg',
            urls.apiUrl+'up/admin/0_1562652106.jpg',
        ],
        hiddenItem: false,
        sharetip: false, //分享弹窗
        container: "",
        statusNum: '',
        currentIndex: 0,
        current: 0,
        classCurrent: 0,
        countNumber: {},
        list: [
            
            {
                img: '/img/emplor.png',
                text1: '拼课管理',
                id: 10,
                imgs: '/img/mored.png',
                path: '/pages/crowdfunding/index/index'
            },
            {
                img: '/img/daili.png',
                text1: '我的联盟',
                id: 11,
                imgs: '/img/mored.png',
                path: '/pages/alliance/index/index'
            },
            {
                img: '/img/myschool.png',
                text1: '我的学校',
                id: 1,
                // text2:'8家',
                imgs: '/img/mored.png',
                path: "/pages/inmyschool/inmyschool"
            },
            {
                img: '/img/emplor.png',
                text1: '老师管理',
                id: 3,
                // text2:'88人',
                imgs: '/img/mored.png',
                path: '/pages/chargemen/chargemen',
                // text2:''
            },
            {
                img: '/img/appoint.png',
                text1: '预约管理',
                id: 4,
                imgs: '/img/mored.png',
                path: '/pages/appointment/index'
            },
            {
                img: '/img/love.png',
                text1: '想学管理',
                id: 9,
                imgs: '/img/mored.png',
                path: '/pages/wantStudy/index'
            },
            {
                img: '/img/erweima.png',
                text1: '下载我的学校二维码',
                imgs: '/img/mored.png',
                id: 5,
                path: "/pages/mycode/mycode?type=school"
            },
            {
                img: '/img/recall.png',
                text1: '反馈建议',
                imgs: '/img/mored.png',
                id: 6
            },
            // {
            // 	img: '/img/business.png',
            // 	text1: '商务合作',
            // 	imgs: '/img/mored.png',
            // 	id: 7
            // },
            // {
            // 	img: '/img/daili.png',
            // 	text1: '成为要培训的城市代理',
            // 	imgs: '/img/mored.png',
            // 	id: 8,
            // 	text2: '共享发展红利'
            // },
        ],
        // text2:'',
        classLists: [{
                name: '在售中',
                num: 0,
                list: []
            },
            {
                name: '待审核',
                num: 0,
                list: []
            },
            {
                name: '已下线',
                num: 0,
                list: []
            },
        ],
        nums: '',
        friendsLists: [{
                "id": 0,
                "name": "消息"
            },
            {
                "id": 1,
                "name": "追踪"
            },
            {
                "id": 2,
                "name": "访客"
            },
        ],
        tabIcon: [{
                id: 0,
                pic: '/img/homes.png',
                piced: '/img/homesd.png',
                title: '首页',
                init: false
            },
            {
                id: 1,
                pic: '/img/message.png',
                piced: '/img/messaged.png',
                title: '信息',
                init: false
            }, {
                id: 2,
                pic: '/img/course.png',
                piced: '/img/coursed.png',
                title: '课程',
                init: false
            },
            {
                id: 3,
                pic: '/img/mine.png',
                piced: '/img/mined.png',
                title: '我的',
                init: false
            },
        ],
        schoolId: '',
        schoolData: {},
        options: {},
        messageInfo: [],
        webUrl: '',
        traceInfo: [],
        tracePage: 1, //追踪页数
        traceTotal: 0, //追踪总页数
        visitorList: [], //访客列表
        visitorPage: 1, //访客页数
        visitorTotal: 0, //访客总页数
    },
    //首页初始化
    home_init() {

    },
    //信息初始化
    message_init() {
        
    },
    //课程初始化
    course_init() {

    },
    my_init() {

    },
    //获取机构审核状态
    schoolAdminStatus: function(callback) {
        let that = this
        let schoolStatus = loginFn()
        wx.request({
            url: urls.schoolAdminStatus,
            data: schoolStatus,
            success: function(res) {
                if(res.data.code == 0) {
                    wx.setStorageSync('schoolId', res.data.data.schoolId);

                    that.setData({
                        statusNum: res.data.data.status,
                        schoolId: res.data.data.schoolId
                    })

                    if(res.data.data.status != 7) {
                        callback && callback();
                    }
                }
                
            }
        })
    },
    //机构数量统计
    schoolStatus: function() {
        let that = this
        let schoolStatus = loginFn();
        wx.request({
            url: urls.schoolStatus,
            data: schoolStatus,
            success: function(res) {
                var newList = that.data.list
                newList[1].path = newList[1].path + '?schoolId=' + res.data.data.schoolId
                newList[3].path += '?schoolId=' + res.data.data.schoolId
                that.setData({
                    list: newList,
                    schoolId: res.data.data.schoolId,
                    countNumber: res.data.data,
                })
                that.schoolInfo(res.data.data.schoolId)
                that.courseList(res.data.data.schoolId, 3)

            }
        })
    },
    //机构信息
    schoolInfo: function(id) {
        let that = this;
        app.request({
            url: 'school/detail',
            data: {
                schoolId: id
            },
            sCallback: (res) => {
                that.setData({
                    alldetail: res.data
                })
            }
        });
    },
    // 底部导航切换
    toPage: function(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            currentIndex: e.currentTarget.dataset.id,
        })
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        })
    },
    //  信息顶部导航的点击
    seCurrent: function(e) {
        this.setData({
            current: e.currentTarget.dataset.id,
        })
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        })
    },
    // 课程的点击
    myclass: function(e) {
        this.setData({
            classCurrent: e.currentTarget.dataset.index
        })
    },
    //我的菜单跳转
    infoDetail: function(e) {
        let id = e.currentTarget.dataset.id;
        let path = e.currentTarget.dataset.path;
        if (id == 3 || id == 1 || id == 10) {
            path += '?schoolId=' + this.data.schoolId;
        } else if (id == 9) {
            path += '?type=school&d=' + this.data.schoolId;
        }
        wx.navigateTo({
            url: path
        })
    },
    //今日访客
    todayVisi: function() {
        this.setData({
            currentIndex: 1,
            current: 2
        })
    },
    // 今日预约
    appointment: function() {
        wx.navigateTo({
            url: '/pages/appointment/index',
        })
    },

    // 课程数
    courseNum: function() {
        this.setData({
            currentIndex: 2
        })
    },
    // 机构数
    jigouNum: function() {
        var that = this;
        wx.navigateTo({
            url: '/pages/inmyschool/inmyschool?schoolId=' + that.data.schoolId,
        })
    },
    // 老师人数
    teacherNum: function() {
        var that = this;
        wx.navigateTo({
            url: '/pages/chargemen/chargemen?schoolId=' + that.data.schoolId,
        })
    },

    //添加课程
    addEditClass: function() {
        wx.navigateTo({
            url: '/pages/addclass/index?schoolId=' + this.data.schoolId + '&schoolName=' + this.data.alldetail.schoolName
        })
    },
    //去聊天界面
    goChat: function(e) {
        let uid = e.currentTarget.dataset.uid;
        let name = e.currentTarget.dataset.name;
        let avatar = e.currentTarget.dataset.avatar;
        wx.navigateTo({
            url: '/pages/chat/chat?uid=' + uid + '&name=' + name + '&avatar=' + avatar + '&from=admin&schoolId='+this.data.schoolId
        })
    },
    //活动详情
    classDetail(e) {
        let index = app.getBindParam(e, 'index');
        let active = this.data.active[index];

        wx.navigateTo({
            url: '/pages/classdetail/index?courseId='+active.courseId+'&active_type='+active.active_type
        })
    },
    //活動項目點擊詳情
    infoclass: function(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/infoclass/infoclass?courseId=' + id,
        })
    },
    //活动
    instiActity: function() {
        let that = this;
        app.request({
            url: 'school/activity',
            data: {
                school_id: that.data.schoolId
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        active: res.data
                    })
                }
            }
        });
    },
    
    //消息
    messageList: function() {
        let that = this;
        app.request({
            url: 'message/listUser',
            sCallback: (res) => {
                if(res.code == 1) {
                    that.setData({
                        messageInfo: res.data
                    })
                }
            }
        })
    },
    //追蹤
    traceList: function() {
        let that = this;
        let page = this.data.tracePage;
        let traceInfo  = this.data.traceInfo;
        if (page > 1 && traceInfo.length >= this.data.traceTotal) {
            return false;
        }

        app.request({
            url: 'visitor/trace',
            data: {
                type: 'school',
                page: page
            },
            sCallback: (res) => {
                if(res.code == 1) {
                    page ++;
                    traceInfo = traceInfo.concat(res.data.list);
                    that.setData({
                        traceInfo: traceInfo,
                        tracePage: page,
                        traceTotal: res.data.total_size
                    })
                }
            }
        })
    },
    // 訪客
    visitor: function() {
        let that = this;
        let page = this.data.visitorPage;
        let visitorList = this.data.visitorList;

        if (page > 1 && visitorList.length >= that.data.visitorTotal) {
            return false;
        }

        app.request({
            url: 'visitor/index',
            data: {type: 'school'},
            sCallback:(res) => {
                if(res.code == 1) {
                    page ++;
                    visitorList = visitorList.concat(res.data.list);
                    that.setData({
                        page: page,
                        visitorList: visitorList,
                        visitorTotal: res.data.total_size
                    })
                }
            }
        })
    },
    //访客详情
    govisitor: function(e) {
        wx.navigateTo({
            url: '/pages/visitor/visitor?uid=' + e.currentTarget.dataset.uid+'&schoolId='+this.data.alldetail.schoolId+'&type=school',
        })
    },
    //课程
    courseList: function(id, status) {
        let that = this
        let courseList = loginFn()
        courseList.schoolId = id
        // courseList.status = status
        courseList.isAdmin = 1
        wx.request({
            url: urls.courseList,
            data: courseList,
            success: function(res) {
                var classLists = [{
                        name: '在售中',
                        num: 0,
                        list: []
                    },
                    {
                        name: '待审核',
                        num: 0,
                        list: []
                    },
                    {
                        name: '已下线',
                        num: 0,
                        list: []
                    },
                ];
                if (res.data.code == '0' && res.data.data.length > 0) {
                    let courseData = res.data.data;
                    var couseLen = courseData.length;
                    for (var i = 0; i < couseLen; i++) {
                        if (courseData[i]['status'] == 3) {
                            classLists[0]['num']++;
                            classLists[0]['list'].push(courseData[i]);
                        } else if (courseData[i]['status'] == 0) {
                            classLists[1]['num']++;
                            classLists[1]['list'].push(courseData[i]);
                        } else if (courseData[i]['status'] == 5) {
                            classLists[2]['num']++;
                            classLists[2]['list'].push(courseData[i]);
                        }
                    }

                }
                that.setData({
                    classLists: classLists,
                })

            }
        })
    },
    // 我的
    teacherMine: function() {
        let that = this
        let teacherMine = loginFn()
        wx.request({
            url: urls.teacherMine,
            data: teacherMine,
            success: function(res) {
                // console.log(res, '+++++++++++++++++++++++')
                // res.data.data.avatar = urls.zhaopeixun + res.data.data.avatar
                that.setData({
                    mineInfo: res.data.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('options:',options);
        this.setData({
            options: options,
            webUrl: urls.zhaopeixun
        })
        console.log('options:',options)

        if(options.scene) {
            var params = app.getSceneData(options.scene);
            if(params['f']) {
                options.fromUid = params['f'];
            }
        }

        //缓存推荐人
        if(options.fromUid) {
            wx.setStorageSync('fromUid', options.fromUid)
        }


        let that = this
        if (options.tijiao) {
            that.setData({
                currentIndex: 3,
                hiddenItem: !that.data.hiddenItem,
            })
        }
        if (options.go) {
            that.setData({
                currentIndex: 2
            })
        }


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    checkLogin() {
        var login = setInterval(() => {
            // console.log('checklogin')
            if (wx.getStorageSync('login')) {
                clearInterval(login);
                this.init();
            }
        }, 100);
    },
    init(callback) {
        this.setData({
            traceInfo: [],
            tracePage: 1, //追踪页数
            traceTotal: 0, //追踪总页数
            visitorList: [], //访客列表
            visitorPage: 1, //访客页数
            visitorTotal: 0, //访客总页数
        })
        this.schoolAdminStatus(() => {
            this.instiActity();
            this.schoolStatus();
            this.messageList();
            this.traceList();
            this.visitor();
            this.teacherMine();

            callback && callback();
        })
        
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.checkLogin();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.init(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.currentIndex == 1){

            if(this.data.current == 1) {
            //追踪分布
                this.traceList();
            } else if(this.data.current == 2) {
                this.visitor();
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        this.setData({
            sharetip: false
        })
        let userInfo = loginFn();
        return {
            title: '你快用这个招生神器！效果真好！',
            imageUrl: '/img/share_school.jpg',
            path: '/pages/institucharge/institucharge?fromUid='+userInfo.loginUid
        }
    },
    shareFn: function() { //分享弹框
        this.setData({
            sharetip: true
        })
    },
    shareCircle: function() { //分享到朋友圈
        wx.navigateTo({
            url: '/pages/share/index?type=school&from=admin',
        })
    },
    //隐藏mask
    hideMask() {
        this.setData({
            sharetip: false
        })
    }
})